package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.GroupModel;
import com.hangzhou.xyj.model.UserModel;
import com.hangzhou.xyj.net.handler.GetGroupMemberHandler;
import com.hangzhou.xyj.net.handler.GetGroupsInfoHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetGroupMemberReq;
import com.hangzhou.xyj.net.request.GetGroupsInfoReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.widget.CircleImageView;
import com.hangzhou.xyj.ui.widget.views.MyTextView;
import com.hangzhou.xyj.utils.constants.DataKey;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;

/**
 * 群资料页面
 * <p/>
 * Created by Administrator on 2016/1/25.
 */
public class GroupInfoActivity extends BaseActivity {
    private CircleImageView mCivGroupHeadIcon;//群图标
    private CircleImageView mCivGroupLeaderHeadIcon;//群主头像
    private CircleImageView mCivGroupMemberHeadIcon1;//群成员头像1
    private CircleImageView mCivGroupMemberHeadIcon2;//群成员头像2
    private CircleImageView mCivGroupMemberHeadIcon3;//群成员头像3
    private CircleImageView mCivGroupMemberHeadIcon4;//群成员头像4
    private CircleImageView mCivGroupMemberHeadIcon5;//群成员头像5
    private CircleImageView mCivGroupMemberHeadIcon6;//群成员头像6
    private CircleImageView mCivGroupMemberHeadIcon7;//群成员头像7
    private MyTextView mTvGroupName;//群名称
    private MyTextView mTvGroupMemberNum;//群成员数量
    private MyTextView mTvGroupId;//群ID
    private MyTextView mTvGroupIntroduce;//群简介
    private MyTextView mTvGroupType;//群类别
    private MyTextView mTvGroupCreateTime;//群创建时间
    private LinearLayout mLlGroupMember;//群成员区域

    private String EMGroupId;//环信Group的Id
    private ImageLoader mImageLoader;//图片加载器
    private String mGroupId;//群id
    private String mGroupMasterId;//群主ID

    @Override
    public void initIntentData(Intent intent) {
        EMGroupId = intent.getStringExtra(DataKey.INDEX);
    }

    @Override
    public void initDatas() {
        mImageLoader = ImageLoader.getInstance();
        getGroupInfo();
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_group_info);
    }

    @Override
    public void initListeners() {
        mLlGroupMember.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentManager.getInstance().goGroupMemberActivity(mContext, mGroupId, EMGroupId, mGroupMasterId);
            }
        });
    }

    @Override
    public void initTop() {
        enableTitle(true, "群名称");
        enableRefresh(true);
    }

    @Override
    public void initViews() {
        mCivGroupHeadIcon = findView(R.id.civ_group_head);//群图标
        mCivGroupLeaderHeadIcon = findView(R.id.civ_group_leader_head);//群主头像
        mCivGroupMemberHeadIcon1 = findView(R.id.civ_group_member_head1);//群成员头像1
        mCivGroupMemberHeadIcon2 = findView(R.id.civ_group_member_head2);//群成员头像2
        mCivGroupMemberHeadIcon3 = findView(R.id.civ_group_member_head3);//群成员头像3
        mCivGroupMemberHeadIcon4 = findView(R.id.civ_group_member_head4);//群成员头像4
        mCivGroupMemberHeadIcon5 = findView(R.id.civ_group_member_head5);//群成员头像5
        mCivGroupMemberHeadIcon6 = findView(R.id.civ_group_member_head6);//群成员头像6
        mCivGroupMemberHeadIcon7 = findView(R.id.civ_group_member_head7);//群成员头像7
        mTvGroupName = findView(R.id.mtv_group_name);//群名称
        mTvGroupMemberNum = findView(R.id.mtv_group_members);//群成员数量
        mTvGroupId = findView(R.id.mtv_group_id);//群ID
        mTvGroupIntroduce = findView(R.id.mtv_group_introduce);//群简介
        mTvGroupType = findView(R.id.mtv_group_cls);//群类别
        mTvGroupCreateTime = findView(R.id.mtv_group_create_time);//群创建时间
        mLlGroupMember = findView(R.id.ll_group_member);//群成员区域
    }

    private void setBaseData(GroupModel model) {//设置群成员以外的其他信息
        mImageLoader.displayImage(model.getGroupImage(), mCivGroupHeadIcon);
        mTvGroupName.setText(model.getGroupName());
        mTvGroupMemberNum.setText(String.valueOf(model.getMemberNum()));
        mTvGroupId.setText(model.getId());
        mImageLoader.displayImage(model.getGroupMasterIcon(), mCivGroupLeaderHeadIcon);
        mTvGroupIntroduce.setText(model.getGroupDesc());
        mTvGroupType.setText(model.getGroupCls());
        mTvGroupCreateTime.setText(model.getCreateTime());
        enableTitle(true, model.getGroupName());
    }

    private void setGroupMemberData(List<UserModel> userList) {
        //反转集合数据
        Collections.reverse(userList);
        int size = userList.size();
        for (int i = 0; i < 7; i++) {
            try {
                if (i > size - 1) {
                    break;
                }
                Field field = this.getClass().getDeclaredField("mCivGroupMemberHeadIcon" + (i + 1));
                field.setAccessible(true);
                CircleImageView circleImageView = (CircleImageView) field.get(this);//反射
                circleImageView.setVisibility(View.VISIBLE);
                mImageLoader.displayImage(userList.get(i).getHeadPic(), circleImageView);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取群成员以外的其他基本信息
     */
    public void getGroupInfo() {
        GetGroupsInfoReq getGroupsInfoReq = new GetGroupsInfoReq(EMGroupId);
        HttpLoader.getInstance().sendRequest(new GetGroupsInfoHandler(getGroupsInfoReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                GroupModel groupModel = ((List<GroupModel>) model).get(0);
                setBaseData(groupModel);
                mGroupId = groupModel.getId();
                mGroupMasterId = groupModel.getGroupMasterId();
                getGroupMember(mGroupId);
            }
        }, 0));
    }

    /**
     * 获取群成员
     *
     * @param groupId 群的id（不是环信groupId）
     * @return
     * @author yaohuix
     * @date 2016/5/9 15:52
     */
    public void getGroupMember(String groupId) {
        GetGroupMemberReq getGroupMemberReq = new GetGroupMemberReq(groupId);
        HttpLoader.getInstance().sendRequest(new GetGroupMemberHandler(getGroupMemberReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                setGroupMemberData((List<UserModel>) model);
            }
        }, 0));
    }
}
